//
//  MainViewController.m
//  
//
//  Created by Apple on 2016/12/31.
//
//
#import "Maincellmodel1.h"
#import "MaincellModel2.h"
#import "MainHeaderCollectionViewCell.h"
#import "Networking.h"
#import "SDCycleScrollView.h"
#import "MainViewController.h"
#import "MainTableViewCell.h"
#import "MainCellModel.h"

@interface MainViewController ()<SDCycleScrollViewDelegate>

@property(nonatomic,strong)UISearchBar *searchBar;
@property(nonatomic,strong)SDCycleScrollView *cycleScrollView;
@property(nonatomic,strong)NSMutableArray *     imagesURLStrings;
@property(nonatomic,strong)NSMutableArray *     arrays1;
@property(nonatomic,strong)NSMutableArray *     arrays2;


@end

@implementation MainViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.view.backgroundColor = [UIColor redColor];
    
    _arrays = [NSMutableArray array];
    _arrays1 = [NSMutableArray array];
    _arrays2 = [NSMutableArray array];

    _imagesURLStrings = [NSMutableArray array];
    [self reloadData:YES];
    [self YDXnaviroigationItem];
    [self customTableView];
     self.tableview.frame=CGRectMake(0,0, SCREEN_WIDTH, SCREEN_HEIGHT-64-49);
    //   UINib *footerNib= [UINib nibWithNibName:@"MainTableViewCell" bundle:nil];
 // [self.tableview registerNib:footerNib                   forCellReuseIdentifier:@"MainTableViewCell"];
    
    [self registerNib:@"MainTableViewCell"];
    WeakSelf(WeakSelf, self);
    [self setTableHeaderRefresh:^{
       [WeakSelf reloadData:YES];
    }];
    
   // [self setFooterfresh:^{
        
    //    [WeakSelf reloadData:NO];
   // }];
}
-(void)reloadData:(BOOL)fresh
{
    //刷新的话page是1  加载page会增加
    if (fresh) {
        self.currentIndex=1;
    }
    else{
        self.currentIndex++;
 
    }
    
    //看pageNumber 的值就是变化的 做刷新或者加载用
//    [NetworkManger getCacheRequestWithUrlString:@"/beetle/inter/activity/getIndexData" parameters:@{@"type":@"1",@"pageNumber":cunrrenIndex,@"displayNumber":@"5"} cacheTime:NO succeed:^(id data) {
//       //结束刷新
//        [self endReFresh];
//        //是刷新的话就全部吧数组内容删除  加载的话不用
//        if (fresh) {
//            [_imagesURLStrings removeAllObjects];
//            
//        }
//    
//        for (NSDictionary *dic in data[@"values"]) {
//            [_imagesURLStrings addObject:dic[@"image"]];
//        }
//        NSLog(@"%@^^^^^^^^^^^",_imagesURLStrings);
//        [self.tableview reloadData];
//        
//        
//    } fail:^(NSError *error) {
//        
//    }];
    
    [NetworkManger getCacheRequestWithUrlString:@"/beetle/inter/activity/getIndexData" parameters:nil  cacheTime:NO succeed:^(id data) {
        NSLog(@"****%@",data);
        [self endReFresh];
        if (fresh) {
            
            [_imagesURLStrings removeAllObjects];
            [self.dataArray removeAllObjects];
        }
        NSDictionary *Listdic = [NSDictionary dictionary];
        Listdic =data[@"values"][@"indexRecommendBack"];
        for (NSDictionary *dic in data[@"values"][@"indexAdvertList"]) {
            [_imagesURLStrings addObject:dic[@"image"]];
            
        }
        for (NSDictionary *dic in Listdic[@"superCustomerList"]){
            MaincellModel2 *model=[MaincellModel2 mj_objectWithKeyValues:dic];
            [_arrays2 addObject:model];
        }
        [self.dataArray addObject:_arrays2];
        
        for (NSDictionary *dic in Listdic[@"scenicList"]){
            MainCellModel *model=[MainCellModel mj_objectWithKeyValues:dic];
            
            [_arrays1 addObject:model];
            
        }
        [self.dataArray addObject:_arrays1];

        for (NSDictionary *dic in Listdic[@"activitiesList"]){
            Maincellmodel1 *model=[Maincellmodel1 mj_objectWithKeyValues:dic];
            [_arrays addObject:model];
        }
            [self.dataArray addObject:_arrays];
        
        
        [self.tableview reloadData];
    } fail:^(NSError *error) {
        [self endReFresh];

    }];

}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 269;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    //这要根据数据个数返回
    return 3;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //
    return 370;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    
//    _cycleScrollView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0,0, SCREEN_WIDTH,200) shouldInfiniteLoop:YES imageNamesGroup:imageNames];
    
    
    

   // _cycleScrollView.delegate = self;
    
    
    _cycleScrollView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 0, ScreenWidth, 200) delegate:self placeholderImage:[UIImage imageNamed:@"placeholder"]];
    _cycleScrollView.imageURLStringsGroup = _imagesURLStrings;
//    _cycleScrollView.titlesGroup=@[@"好风景",@"去你妈的",@"dsds"];
    
    UIImage *iamge1 = [UIImage imageNamed:@"carouse_spot_routine"];
    UIImage *iamge2 = [UIImage imageNamed:@"carouse_spot_selected"];
    _cycleScrollView.currentPageDotImage = iamge2;
    _cycleScrollView.pageDotImage = iamge1;

    _cycleScrollView.pageControlDotSize =CGSizeMake(8, 8);
    _cycleScrollView.scrollDirection = UICollectionViewScrollDirectionVertical;
    _cycleScrollView.backgroundColor = [UIColor blackColor];
    _cycleScrollView.pageControlStyle = SDCycleScrollViewPageContolStyleAnimated;
    _cycleScrollView.scrollDirection =UICollectionViewScrollDirectionHorizontal
    ;
    //--- 轮播时间间隔，默认1.0秒，
    _cycleScrollView.autoScrollTimeInterval = 3;

    
        UIView *view1 = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, 269)];
        view1.backgroundColor = [UIColor whiteColor];
        [view1 addSubview:_cycleScrollView];
    NSArray *array1 = [[NSArray alloc]initWithObjects:@"home_nearby",@"home_medal",@"home_activity",@"home_invitation", nil];
    NSArray *array2 = [[NSArray alloc]initWithObjects:@"附近",@"达人",@"活动",@"请柬", nil];
    
    for (int i = 0; i<4; i++) {
       
        UIView *titleview = [[UIView alloc]initWithFrame:CGRectMake((ScreenWidth/4)*i, _cycleScrollView.bottom, ScreenWidth/4, 69)];
        //titleview.backgroundColor  = [UIColor redColor];
        UIImageView *imageview = [[UIImageView alloc]initWithFrame:CGRectMake(30,14, 24.5, 23.5)];
        UIImage *image = [UIImage imageNamed:array1[i]];
        imageview.image = image;
        UILabel *lables = [[UILabel alloc]initWithFrame:CGRectMake(30,43.5, 60, 7)];
        lables.text = array2[i];
        lables.font = [UIFont systemFontOfSize:12];
        lables.textAlignment = NSTextAlignmentLeft;
        [titleview addSubview:imageview];
        [titleview addSubview:lables];
        titleview.userInteractionEnabled = YES;
        UITapGestureRecognizer *top = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(topAction:)];
        [view1 addSubview:titleview];
       
    }
    return view1;
   }

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MainTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MainTableViewCell" forIndexPath:indexPath];
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
       // [cell cellFuZHiWithModel:indexPath];
    if (indexPath.row == 0) {
        if (self.dataArray.count>0) {
            NSLog(@"!!!!!!!!!!!%@",self.dataArray);
            [cell cellFuZHiWithModel:indexPath mode:self.dataArray[0]];
             NSLog(@"%@!!!!!!!!!!!!!!!!",self.dataArray[0]);
        }
    }
    if (indexPath.row == 1) {
        if (self.dataArray.count >1) {
            [cell cellFuZHiWithModel:indexPath mode:self.dataArray[1]];

        }
    }if (indexPath.row == 2) {
        if (self.dataArray.count>2) {
            [cell cellFuZHiWithModel:indexPath mode:self.dataArray[2]];
        }
    }
        return cell;

   //}
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NSLog(@"基类里输出请重写方法");
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
  
}

-(void)YDXnaviroigationItem{
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 43, 15)];
    view.backgroundColor = [UIColor whiteColor];
    UILabel *lable = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 33, 15)];
    lable.text = @"太原";
    lable.font = [UIFont systemFontOfSize:13];
    lable.textColor = UIColorFromRGB(0x141414);
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(lable.right+1.5,lable.top+5 , 9.5, 4.5)];
    UIImage *image = [UIImage imageNamed:@"home_nav_dropdown"];
    imageView.image = image;
    [view addSubview:imageView];
    [view addSubview:lable];
    UIBarButtonItem *rightview = [[UIBarButtonItem alloc]initWithCustomView:view];
    self.navigationItem.leftBarButtonItem = rightview;
    UIImageView*iamgeview2 = [[UIImageView alloc]initWithFrame:CGRectMake(view.right+22,view.top-7,ScreenWidth-view.right+22-100, 27*SHIJI_HEIGHT)];
    UIImage *image2 = [UIImage imageNamed:@"home_nav_search"];
    iamgeview2.image = image2;
    UIBarButtonItem *titview = [[UIBarButtonItem alloc]initWithCustomView:iamgeview2];
    self.navigationItem.titleView.backgroundColor = [UIColor blueColor];
    [self.navigationController.navigationBar addSubview:iamgeview2];
    [self addRightBtnWithframe:CGRectMake(0, 0, 18, 18) withImage:@"home_nav_information_03" ishidden:YES];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}



@end
